package com.zhonghan.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.zhonghan.recharge.pojo.ZhBusinessUser;

public class BusinessLoginInterceptor implements HandlerInterceptor {

	
	/**
	 * 拦截器执行之前调用的方法
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 获取请求进来的url
		String url = request.getRequestURI();
		// 判断url请求的路径是否是需要被拦截的
		if(url.contains("/business/login")){
			return true;
		}
		
		// 获取session
		HttpSession session = request.getSession();
		ZhBusinessUser user = (ZhBusinessUser) session.getAttribute("zhonghan_business_user");
		if(user != null){
			return true;
		}
		
		// 没有登录的就跳转到登录页面
		request.getRequestDispatcher("/WEB-INF/views/business/login.jsp").forward(request, response);
		
		return false;
	}

	/**
	 * 拦截器执行之后,视图返回前调用的方法
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		

	}

	/**
	 * 拦截器执行之后调用的方法
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
